package com.zmj.controller;

import com.google.code.kaptcha.Producer;
import com.zmj.dto.SysUserDTO;
import com.zmj.dto.TableData;
import com.zmj.entity.SysUser;
import com.zmj.service.SysUserService;
import com.zmj.utils.Response;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.List;

@RestController
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private Producer producer;

    @RequestMapping("sys/login")
    public Response login(@RequestBody SysUserDTO userDTO, HttpSession session){
          String  c=userDTO.getCaptcha();
          if (c==null){
            return Response.error("验证码为空");
          }
          String  b=(String) session.getAttribute("code");
          if (!c.equals(b)){
              return Response.error("验证码错误");
          }

          Response response=sysUserService.login(userDTO);

          SysUser sysUser=(SysUser) response.get("sysUser");

          session.setAttribute("sysUser",sysUser);

          return response;
    }


    @RequestMapping("captcha.jpg")
    public  void createKaptcha(HttpServletResponse response, HttpSession session){

            try {
                String code = producer.createText();
                System.out.println("code:" + code);
                session.setAttribute("code", code);

                BufferedImage bufferedImage = producer.createImage(code);

                ImageIO.write(bufferedImage, "jpg", response.getOutputStream());

            }catch (Exception e){
                e.printStackTrace();
            }
    }
    @RequestMapping("/sys/user/list")
    public TableData userList(String search, String order, int limit, int offset){

        return  sysUserService.findUserBypage(search,order,offset,limit);
    }

    @RequestMapping("/sys/user/save")
    public  Response save(@RequestBody SysUser sysUser){
        sysUser.setCreateTime(new Date());
        return  sysUserService.savaUser(sysUser);
    }


    @RequestMapping("/sys/user/info/{userId}")
    public  Response userInfo(@PathVariable long userId){
        return  sysUserService.findUser(userId);
    }

    @RequestMapping("/sys/user/update")
    public  Response update(@RequestBody SysUser sysUser){

        return  sysUserService.updateUser(sysUser);
    }


    @RequestMapping("/sys/user/del")
    public  Response  del(@RequestBody List<Long> ids){
        return  sysUserService.del(ids);
    }

    @RequestMapping("/sys/user/info")
    public  Response info(HttpSession session){
        return Response.success().put("user",(SysUser)session.getAttribute("sysUser"));
    }
    @RequestMapping("sys/user/logout")
    public  Response logout(HttpSession session){
        session.invalidate();//session失效

        return Response.success();
    }


    @RequestMapping("/sys/user/userList")
    public  Response  allUsers(){

        return  sysUserService.findAll();
    }

}
